import 'package:flutter/material.dart';
import 'package:fluttermodule/customerDetail/customer_detail_model.dart';
import 'package:fluttermodule/customerInfo/customer_info.dart';
import 'package:fluttermodule/provide/customer_detail.dart';
import 'package:provide/provide.dart';

import 'customer_detail_view_model.dart';

class CustomerDetailPage extends StatefulWidget {
  String code;
  String name;
  CustomerDetailPage({Key key,this.code,this.name}):super(key:key);
  @override
  _CustomerDetailPageState createState() => _CustomerDetailPageState();
}

class _CustomerDetailPageState extends State<CustomerDetailPage> {
  List<CustomerDetailViewModel> listData = [];
  List<CustomerDetailViewModel> simpleList = [];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('顾客详情'),
        backgroundColor: Color(0xff797979),
        centerTitle: true,
        leading: IconButton(icon:Icon(Icons.arrow_back_ios),
        onPressed:(){
              Navigator.pop(context);
            }
          ),

        elevation: 0,
      ),
      body: FutureBuilder(
        future: _getCustomerDetail(context),
        builder: (context, snapshot) {
          return _getListBody();
        },
      ),
      backgroundColor: Color(0xffffffff),
    );
  }

  _getListBody() {
    listData.clear();
    listData.addAll(Provide.value<CustomerDetailProvide>(context).listData);
    if (listData.isEmpty) {
      simpleList.add(CustomerDetailViewModel(
          CustomerDetailViewModel.TYPE_HEADER,
          customerDetailHeader: CustomerDetailHeader()));
      simpleList.add(CustomerDetailViewModel(
          CustomerDetailViewModel.TYPE_FOOTER,
          customerDetailFooter: CustomerDetailFooter()));
      return Stack(
        children: <Widget>[
          ListView.builder(
              itemCount: simpleList.length,
              itemBuilder: (BuildContext content, int position) {
                return _getItem(position);
              }),
          Center(child: CircularProgressIndicator())
        ],
      );
    } else {
      return ListView.builder(
          itemCount: (listData == null) ? 0 : listData.length,
          itemBuilder: (BuildContext content, int position) {
            return _getItem(position);
          });
    }
  }

  _getItem(int position) {
    CustomerDetailViewModel customerDetailViewModel =
        listData.isEmpty ? simpleList[position] : listData[position];
    switch (customerDetailViewModel.type) {
      case CustomerDetailViewModel.TYPE_HEADER:
        return Header(customerDetailViewModel.customerDetailHeader);
      case CustomerDetailViewModel.TYPE_FOOTER:
        return Footer(customerDetailViewModel.customerDetailFooter);
      case CustomerDetailViewModel.TYPE_CONTENT:
        return Content(customerDetailViewModel.customerDetailContent);
    }
  }

  Future _getCustomerDetail(BuildContext context) async {
    try {
      await Provide.value<CustomerDetailProvide>(context).getCustomerDetail();
    } catch (e) {
      print(e);
    }
  }
}

class Header extends StatelessWidget {
  final CustomerDetailHeader customerDetailHeader;

  Header(this.customerDetailHeader);

  @override
  Widget build(BuildContext context) {
    return Column(children: [
      Container(
        width: MediaQuery.of(context).size.width,
        padding: EdgeInsets.fromLTRB(20, 0, 20, 6),
        height: 181.0,
        color: Color(0xff797979),
        child: Column(
          children: <Widget>[
          InkWell(
          onTap: (){
              Navigator.push(context, MaterialPageRoute(builder: (context) {
                return CustomerInfoPage();
              }));
            },
            child: Container(
              width: MediaQuery.of(context).size.width,
              height: 115,
              color: Color(0xff939393),
              child: Container(
                padding: EdgeInsets.all(20),
                child: Column(
                  children: <Widget>[
                    Stack(
                      children: <Widget>[
                        Align(
                          alignment: Alignment.topLeft,
                          child: Row(
                            children: <Widget>[
                              Icon(
                                Icons.headset_mic,
                                size: 36,
                              ),
                              Container(
                                margin: EdgeInsets.fromLTRB(10, 0, 0, 0),
                                child: Column(
                                    mainAxisSize: MainAxisSize.min,
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: <Widget>[
                                      Container(
                                        child: Text(
                                          customerDetailHeader.nickName == null
                                              ? ''
                                              : customerDetailHeader.nickName,
                                          style: TextStyle(
                                              color: Color(0xffffffff),
                                              fontSize: 14),
                                        ),
                                      ),
                                      Container(
                                        margin: EdgeInsets.fromLTRB(0, 5, 0, 0),
                                        child: Text(
                                          customerDetailHeader.cardNumber ==
                                                  null
                                              ? ''
                                              : customerDetailHeader.cardNumber,
                                          style: TextStyle(
                                              color: Color(0xffffffff),
                                              fontSize: 12),
                                        ),
                                      )
                                    ]),
                              ),
                            ],
                          ),
                        ),
                        Align(
                          alignment: Alignment.bottomRight,
                          child: Padding(
                              padding: EdgeInsets.only(top: 12),
                              child: Icon(
                                Icons.arrow_forward_ios,
                                color: Color(0xffd8d8d8),
                                size: 16,
                              )),
                        ),
                      ],
                    ),
                    Container(
                      margin: EdgeInsets.fromLTRB(0, 20, 0, 0),
                      width: MediaQuery.of(context).size.width,
                      child: Text(
                        customerDetailHeader.description == null
                            ? ''
                            : customerDetailHeader.description,
                        style:
                            TextStyle(color: Color(0xffffffff), fontSize: 12),
                      ),
                    )
                  ],
                ),
              ),
            )
          ),

            Container(
              margin: EdgeInsets.fromLTRB(0, 8, 0, 0),
              padding: EdgeInsets.fromLTRB(20, 0, 20, 0),
              width: MediaQuery.of(context).size.width,
              height: 40,
              color: Color(0xff939393),
              child: Stack(
                children: <Widget>[
                  Align(
                      alignment: Alignment.centerLeft,
                      child: Text(
                        customerDetailHeader.workRecord == null
                            ? ''
                            : customerDetailHeader.workRecord,
                        style:
                            TextStyle(color: Color(0xffffffff), fontSize: 12),
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                      )),
                  Align(
                      alignment: Alignment.centerRight,
                      child: Icon(
                        Icons.arrow_forward_ios,
                        color: Color(0xffd8d8d8),
                        size: 16,
                      )),
                ],
              ),
            ),
          ],
        ),
      ),
      Container(
        width: MediaQuery.of(context).size.width,
        height: 50,
        padding: EdgeInsets.fromLTRB(20, 0, 20, 0),
        child: Stack(children: [
          Align(
              alignment: Alignment.centerLeft,
              child: Text('温馨提示',
                  style: TextStyle(color: Color(0xff000000), fontSize: 14))),
          Align(
              alignment: Alignment.centerRight,
              child: Row(
                mainAxisSize: MainAxisSize.min,
                mainAxisAlignment: MainAxisAlignment.start,
                children: <Widget>[
                  Text('查看全部',
                      style: TextStyle(color: Color(0xffa9a9a9), fontSize: 13)),
                  Icon(
                    Icons.arrow_forward_ios,
                    color: Color(0xffd8d8d8),
                    size: 16,
                  )
                ],
              )),
        ]),
      ),
    ]);
  }
}

class Footer extends StatelessWidget {
  final CustomerDetailFooter customerDetailFooter;

  Footer(this.customerDetailFooter);

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.start,
      children: <Widget>[
        Container(
          width: MediaQuery.of(context).size.width,
          color: Color(0xfff5f5f5),
          height: 8,
        ),
        Container(
          padding: EdgeInsets.fromLTRB(20, 16, 20, 25),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              Container(
                padding: EdgeInsets.fromLTRB(0, 0, 0, 16),
                width: MediaQuery.of(context).size.width,
                child: Text(
                  '顾客动态',
                  style: TextStyle(color: Color(0xff000000), fontSize: 13),
                ),
              ),
              Container(
                padding: EdgeInsets.all(20),
                width: MediaQuery.of(context).size.width,
                color: Color(0xfff4f4f4),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    Container(
                      child: Column(
                        children: <Widget>[
                          Text(customerDetailFooter.money == null
                              ? ''
                              : customerDetailFooter.money),
                          Text(
                            '本月购货(元)',
                            style: TextStyle(
                                fontSize: 12, color: Color(0xffd8d8d8)),
                          )
                        ],
                      ),
                    ),
                    Container(
                      child: Column(
                        children: <Widget>[
                          Text(customerDetailFooter.count == null
                              ? ''
                              : customerDetailFooter.count),
                          Text(
                            '本月国内进修指数',
                            style: TextStyle(
                                fontSize: 12, color: Color(0xffd8d8d8)),
                          )
                        ],
                      ),
                    ),
                    Container(
                      child: Column(
                        children: <Widget>[
                          Text(customerDetailFooter.count1 == null
                              ? ''
                              : customerDetailFooter.count1),
                          Text(
                            '本月新增消费者',
                            style: TextStyle(
                                fontSize: 12, color: Color(0xffd8d8d8)),
                          )
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
        Container(
          height: 8,
          color: Color(0xfff5f5f5),
        ),
        Container(
          padding: EdgeInsets.fromLTRB(20, 14, 20, 10),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              Container(
                width: MediaQuery.of(context).size.width,
                child: Stack(
                  children: <Widget>[
                    Align(
                      alignment: Alignment.topLeft,
                      child: Text('长客会'),
                    ),
                    Align(
                        alignment: Alignment.topRight,
                        child: Icon(
                          Icons.arrow_forward_ios,
                          color: Color(0xffd8d8d8),
                          size: 16,
                        ))
                  ],
                ),
              ),
            ],
          ),
        ),
        Container(
          padding: EdgeInsets.fromLTRB(20, 0, 20, 0),
          child: Divider(color: Color(0xffd8d8d8), height: 2, indent: 1),
        ),
        Container(
          padding: EdgeInsets.fromLTRB(20, 12, 20, 10),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              Container(
                width: MediaQuery.of(context).size.width,
                child: Stack(
                  children: <Widget>[
                    Align(
                      alignment: Alignment.topLeft,
                      child: Text('货代记录'),
                    ),
                    Align(
                        alignment: Alignment.topRight,
                        child: Icon(
                          Icons.arrow_forward_ios,
                          color: Color(0xffd8d8d8),
                          size: 16,
                        ))
                  ],
                ),
              ),
            ],
          ),
        ),
        Container(
          padding: EdgeInsets.fromLTRB(20, 0, 20, 0),
          child: Divider(color: Color(0xffd8d8d8), height: 2, indent: 1),
        ),
        Container(
          padding: EdgeInsets.fromLTRB(20, 12, 20, 10),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              Container(
                width: MediaQuery.of(context).size.width,
                child: Stack(
                  children: <Widget>[
                    Align(
                      alignment: Alignment.topLeft,
                      child: Text('他们授权'),
                    ),
                    Align(
                      alignment: Alignment.topRight,
                      child: Icon(
                        Icons.arrow_forward_ios,
                        color: Color(0xffd8d8d8),
                        size: 16,
                      ),
                    )
                  ],
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }
}

class Content extends StatelessWidget {
  final CustomerDetailContent customerDetailContent;

  Content(this.customerDetailContent);

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.fromLTRB(20, 0, 0, 0),
      child: Column(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            Container(
                padding: EdgeInsets.fromLTRB(0, 0, 0, 3),
                width: MediaQuery.of(context).size.width,
                child: Text(
                  customerDetailContent.date,
                  style: TextStyle(color: Color(0xff000000), fontSize: 10),
                )),
            Container(
                width: MediaQuery.of(context).size.width,
                child: Text(
                  customerDetailContent.content,
                  style: TextStyle(color: Color(0xffc0c0c0), fontSize: 11),
                )),
            Divider(
                color: customerDetailContent.isLast
                    ? Color(0xffffffff)
                    : Color(0xffd8d8d8),
                height: 14,
                indent: 0.5)
          ]),
    );
  }
}
